package niuke;
import  java.util.*;
class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val){
        this.val=val;
    }
}
class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
public class niuKeMain5 {
    public int atoi (String str) {
        // write code here
        if (str==null){
            return 0;
        }
        String str1="";
        int count=0;
        for (int i = 0; i < str.length(); i++) {
            char ch=str.charAt(i);
            if (ch==' '&&count==0){
                continue;
            }
            if (Character.isLetter(ch)&&count==0){
                return 0;
            }
            else if (ch>='0'&&ch<='9'||ch=='-'){
                str1+=ch;
                count++;
            }
            else if (Character.isLetter(ch)&&count!=0){
                break;
            }
        }
        if(str1==""){
            return 0;
        }
        int sum=Integer.parseInt(str1);
        return sum;
    }
    public boolean solve (String A, String B) {
        // write code here
        if (A==null&&B==null||A.equals(B)){
            return true;
        }
        if (A==null&&B!=null){
            return false;
        }
        if(A!=null&&B==null){
            return false;
        }
        if (A.length()!=B.length()){
            return false;
        }
        char ch=A.charAt(0);
        int count=0;
        for (int i = 0; i < B.length(); i++) {
            if (ch==B.charAt(i)){
                count=i;
                break;
            }
        }
        char[] ch1=A.toCharArray();
        for (int i = 0; i < count; i++) {
            char ch2=ch1[A.length()-1];
            int j=0;
            for (j = ch1.length-1; j >=0; j--) {
                ch1[j]=ch1[j-1];
            }
            ch1[0]=ch2;
        }
        char[] chars=B.toCharArray();
        if (Arrays.equals(ch1,chars)){
            return true;
        }
//        String C=Arrays.toString(ch1);
//        System.out.println(C);
//        if (C.equals(B)){
//            return true;
//        }
        return false;
    }
    public ListNode reverseKGroup (ListNode head, int k) {
        // write code here
        if (head==null||k<1){
            return null;
        }
        Stack<ListNode> stack=new Stack<>();
        ListNode cur=head;
        int count=0;
        cur=head;
        ListNode node=new ListNode(-1);
        ListNode node1=node;
        while (true){
            for (int i = 0; i < k&&cur!=null; i++) {
                stack.push(cur);
                cur=cur.next;
                count++;
            }
            if (count==k){
                while (!stack.isEmpty()){
                    node1.next=stack.pop();
                    node1=node1.next;
                    node1.next=null;
                }
            }
            if (cur==null){
                break;
            }
            count=0;
        }
        ListNode end=null;
        while (!stack.isEmpty()){
            ListNode n1=stack.pop();
            n1.next=end;
            end=n1;
        }
        node1.next=end;
        return node.next;
    }
    public int ysf (int n, int m) {
        // write code here
        int res = 0;
        //最后一轮剩下2个人，所以从2开始反推
        for (int i = 2; i <= n; i++) {
            res = (res + m) % i;
        }
        return res+1;
    }
    public boolean isSametree(TreeNode pleft, TreeNode pright){
        if (pleft==null&&pright==null){
            return true;
        }
        if (pleft==null&&pright!=null){
            return false;
        }
        if (pleft!=null&&pright==null){
            return false;
        }
        if (pleft.val!= pright.val){
            return false;
        }
        return isSametree(pleft.left,pright.right)&&isSametree(pleft.right,pright.left);
    }
    boolean isSymmetrical(TreeNode pRoot) {
        if (pRoot==null){
            return true;
        }
        return isSametree(pRoot.left,pRoot.right);
    }
}
